/**
 * torchd.FB
 *
 * Handles connecting to Facebook and displaying data.
 * This is the conduit between the view controller, the 
 * API model and the user model(s). 
 * All requests from any of those objects to another 
 * object run through this object.
 *
 * @package Torchd Framework
 * @subpackage Facebook
 * @author Jason Raede <jason@torchdm.com>
 */

torchd.FB = {
	
	
	/**
	 * The view object for the controller.
	 *
	 * Must implement the following methods:
	 *	displayUserInfo(userInfo)
	 *	displayUserFriends
	 */
	view:{},
	
	/**
	 * The currently logged-in user
	 * 
	 * An instance of the torchd.FB.user class
	 */
	user:{},
	
	
	/**
	 * Initialize the Facebook connection.
	 *
	 * Connect with the torchd.FB.api object, then call
	 * appropriate methods on the view depending upon response
	 *
	 * @param string appId		The application's Facebook ID
	 * @param string channelUrl	The absolute location of the fb-channel.php file
	 * @param object view		The view controller you want to implement
	 */
	init:function(appId, channelUrl, view) {
		this.view = view;
		this.view.init();
		// This is the only "view-specific" action taken here, just because
		// Facebook needs the fb-root element to have loaded before init
		$(document).ready(function() {
			torchd.FB.api.init(appId, channelUrl, function(response) {
				if(response.authResponse) {
			    	if(response.authResponse.accessToken) {
			    		torchd.FB.api.accessToken = response.authResponse.accessToken;
			    		torchd.FB.conduit.user = new torchd.FB.user(response.authResponse.userID);
			    		torchd.FB.conduit.view.loginSucceeded()   		
			    	}
			    	else {
			    		torchd.FB.conduit.view.loginFailed(response);
			    	}
			    }
			    else {
			    	torchd.FB.conduit.view.loginFailed(response);
			    }
			});
		});
		
	},
	
	/**
	 * Gets information about an object from Facebook's API. 
	 *
	 * Useful if you only need to get an object's info once; 
	 * otherwise, use torchd.FB.user and get data that way to 
	 * reduce server load.
	 *
	 * @param string id			The object's ID or username
	 * @param string fields		Fields you'd like to retrieve. Leave blank for all
	 * @param function callback	Function to run upon retrieval
	 */
	getInfo:function(id, fields, callback) {
		if(fields) fields = '?fields=' + fields;
		else fields = '';
		torchd.FB.api.apiCall('/' + id + fields, callback);
	},
	
	/**
	 * Gets an array of all the user's Facebook friends
	 *
	 * Note that this grabs an array of objects with properties "id" and
	 * "name"
	 *
	 * @param function callback		Callback function when friends are retrieved.
	 */
	getUserFriends:function(callback) {
		this.user.getFriends(callback);
	},
	
	/**
	 * Gets an array of torchd.FB.user objects representing
	 * the current user's friends
	 *
	 * @param int count				Number of friends to get. Advised to keep this low
	 *								to avoid slowdowns
	 * @param function callback		Callback function after retrieving the array
	 */
	getUserFriendsAsObjects:function(count, callback) {
		this.user.getFriends(function(friends) {
			if(!count) count = friends.length;
			var returnArray = new Array();
			for(var i=0;i<count;i++) {
				if(friends[i]) {
					returnArray.push(new torchd.FB.user(friends[i].id));
				}
			}
			callback(returnArray);
		});
	},
	
	
	/**
	 * Returns the URL of a user's profile picture
	 *
	 * @param string size ->
	 *		'square' :: 50x50
	 *		'small'  :: 50x?
	 *		'normal' :: 100x?
	 *		'large'  :: 200x?
	 */
	profilePicture: function(size, id) {
		if(size) size = '?type=' + size;
		return 'http://graph.facebook.com/' + id + '/picture' + size;
	}
	
	
	
		
	
	
};